<?php

class champ_table
{
	var $id;
	var $division_id;
	var $table_id;
	var $display_mode;
	
	var $division_data = Array();
	var $page_header;
	
	var $records = Array();
	
	var $division_options = '';
	var $table_options = '';
	var $display_options = '';
	

	function champ_table($id, $division_id, $table_id, $display_mode = 'overview')
	{
		global $db, $lang;
		include_once('includes/page_header.php');
		
		$this->id = $id;
		$q = 'SELECT * FROM ' . DIVISIONS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ' WHERE c_id = d_champid AND d_champid = ' . $id . ' ORDER BY d_order';
		$res = $db->sql_query($q);
		
		$actual_id = 0;
		while ($row = $db->sql_fetchrow($res))
		{
			if($actual_id == 0)
			{
				$actual_id = $row['d_id'];
				$this->division_data = $row;
			}
			
			if($row['d_id'] == $division_id)
			{
				$actual_id = $division_id;
				$this->division_options .= '<option value="' .  $row['d_id'] . '" SELECTED>'. $row['d_title'].'</option>' ;
				$this->division_data = $row;
			}
			else
			{
				$this->division_options .= '<option value="' .  $row['d_id'] . '">'. $row['d_title'].'</option>' ;
			}
		}
		
		
		$this->display_mode = $display_mode == 'event' ? 'event' : 'overview';
		
		$this->display_options = $display_mode == 'event' ? 
									'<option value="overview">'. $lang['StandingsOverview'].
									'</option><option value="event" SELECTED>'. $lang['StandingsEvent'].'</option>' :
									'<option value="overview" SELECTED>'. $lang['StandingsOverview'].
									'</option><option value="event">'. $lang['StandingsEvent'].'</option>'  ;
		
		$this->division_id = $actual_id;
		$this->table_id = 'drivers';
		$tabletype = $lang['StandingsDriversStandings'];
		if($table_id == 'drivers')
		{
			$this->table_options .= '<option value="drivers" SELECTED>' . $lang['Drivers'] . '</option>';
		}
		else
		{
			$this->table_options .= '<option value="drivers">' . $lang['Drivers'] . '</option>';
		}
		
		if($table_id == 'teams')
		{
			$this->table_id = 'teams';
			$tabletype = $lang['StandingsTeamsStandings'];
			$this->table_options .= '<option value="teams" SELECTED>' . $lang['Teams'] . '</option>';
		}
		else
		{
			$this->table_options .= '<option value="teams">' . $lang['Teams'] . '</option>';
		}
		
		if($table_id == 'manufacturers')
		{
			$this->table_id = 'manufacturers';
			$tabletype = $lang['StandingsManufacturersStandings'];
			$this->table_options .= '<option value="manufacturers" SELECTED>' . $lang['Manufacturers'] . '</option>';
		}
		else
		{
			$this->table_options .= '<option value="manufacturers">' . $lang['Manufacturers'] . '</option>';
		}
		
		$tabletype = $this->division_data['d_title'] == $this->division_data['c_title'] ? $tabletype : 
														$this->division_data['d_title'] . ' ' . $tabletype;
		$this->page_header = new page_header($this->division_data, 'c_title', '', 'c_logoThmbURL', 'd_logoThmb', $tabletype );
											
		if($this->table_id == 'drivers')
		{
			$this->setupDrivers();
		}								
		if($this->table_id == 'teams')
		{
			$this->setupTeams();
		}							
		if($this->table_id == 'manufacturers')
		{
			$this->setupManufacturers();
		}
	}
	
	function setupDrivers()
	{
		global $db, $lang, $image_dir;
		
		$q = 'SELECT *, pf_drivername as user_drivername, 
				(SELECT t.t_id FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs_team LIMIT 0, 1) AS t_id,
				(SELECT t.t_name FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = cs_team LIMIT 0, 1) AS t_name
			  FROM ' . DRIVERSTANDINGS_TABLE . ', ' . USERS_TABLE . ' u, ' . DRIVERNAME_TABLE . ' d, ' . CHAMPSIGNUPS_TABLE . ',' .
			  		   DIVISIONS_TABLE . ', ' . CARS_TABLE . '
			  WHERE u.user_id = d.user_id AND u.user_id = ct_id AND ct_div = d_id AND cs_div = ct_div AND 
			  		cs_driver = u.user_id AND cs_car = c_id AND ct_div = ' . $this->division_id . ' 
			  ORDER BY ct_pos';

		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			$driverinfo = $row['cs_imageThmb'] != '' ? '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . 
												$row['cs_imageThmb'] . '&quot;/><br>': '<p align=&quot;center&quot;>' ;
			$driverinfo .= $row['cs_no'] == 0 ? $row['user_drivername'] . '<br>' : $row['cs_no'] . ' ' . $row['user_drivername'] . '<br>';
			$driverinfo .= $row['t_id'] > 0 ? '<a href=&quot;teams.php?id=' . $row['t_id'] . '&quot;>'. $row['t_name']. '</a>': $lang['Independent'];
			$driverinfo .= '<br><a href=&quot;cars.php?id=' . $row['c_id'] . '&quot;>' . $row['c_manufacturer'] . ' '  .  $row['c_model'] . '</a>';
			$driverinfo .= '</p>';
			
			$row['ct_entrant'] = '<a href="drivers.php?id=' . $row['user_id'] . '" onmouseover="Tip(\'' . $driverinfo . 
						'\', BGCOLOR, \'#FFFFFF\', BORDERCOLOR, \'#000000\', STICKY, true, FADEIN, 500, FADEOUT, 500)" onmouseout="UnTip()">' . 
						$row['user_drivername'] . '</a>';
			//$row['ct_entrant'] = '<a href="drivers.php?id=' . $row['user_id'] . '">' . $row['user_drivername'] . '</a>';
			$this->records[] = $row;
		}
		
	}
	
	function setupTeams()
	{
		global $db, $lang, $image_dir;
		
		$q = 'SELECT *, (SELECT group_concat(DISTINCT pf_drivername SEPARATOR "<BR>")
                                    FROM ' . DRIVERNAME_TABLE . ', ' . EVENTSIGNUPS_TABLE . '
                                    WHERE es_driver = user_id AND es_division = ct_div AND es_team = ct_id) AS t_drivers
				FROM ' . TEAMSTANDINGS_TABLE . ', ' . TEAMS_TABLE . ' WHERE t_id = ct_id AND ct_div = ' . 
						$this->division_id . ' ORDER BY ct_pos';
		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			$teaminfo = $row['t_logo'] != '' ? '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . 
												$row['t_logo'] . '&quot;/><br>': '<p align=&quot;center&quot;>' ;
			$teaminfo .=  $row['t_drivers'] . '</p>';
			
			$row['ct_entrant'] = '<a href="teams.php?id=' . $row['t_id'] . '" onmouseover="Tip(\'' . $teaminfo . 
						'\', BGCOLOR, \'#FFFFFF\', BORDERCOLOR, \'#000000\', STICKY, true, FADEIN, 500, FADEOUT, 500)" onmouseout="UnTip()">' . 
						$row['t_name'] . '</a>';
			$this->records[] = $row;
		}
		
	}
	
	function setupManufacturers()
	{
		global $db, $lang, $image_dir;
		
		$q = 'SELECT * FROM ' . MANUFACTURERSTANDINGS_TABLE . ', ' . CARS_TABLE . ' WHERE c_id = ct_id AND ct_div = ' . 
						$this->division_id . ' ORDER BY ct_pos';
		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			if($row['c_logoThmb'] != '')
			{
				$carlogo =  '<p align=&quot;center&quot;><img src=&quot;' . $image_dir . 
									$row['c_logoThmb'] . '&quot;/><br></p>';
									
				$row['ct_entrant'] = '<a href="cars.php?id=' . $row['c_id'] . '" onmouseover="Tip(\'' . $carlogo . 
						'\', BGCOLOR, \'#FFFFFF\', BORDERCOLOR, \'#000000\', STICKY, true, FADEIN, 500, FADEOUT, 500)" onmouseout="UnTip()">'  .
						 $row['c_manufacturer'] . ' ' . $row['c_model'] . '</a>';
			}
			else
			{
				$row['ct_entrant'] = '<a href="cars.php?id=' . $row['c_id'] . '">' . $row['c_manufacturer'] . ' ' . $row['c_model'] . '</a>';
			}
			$this->records[] = $row;
		}
		
	}
	
	function render()
	{
		global $lang, $skin_dir;
		
		$id = $this->id;
		$divisionOptions = $this->division_options;
		$tableOptions = $this->table_options;
		$displayOptions = $this->display_options;
		$this->page_header->render();
		include $skin_dir . 'tpl/tableChoice.tpl';
		include $skin_dir . 'tpl/tableStart.tpl';
		include $skin_dir . 'tpl/standingHeader.tpl';
		$on = false;
		foreach($this->records as $data)
		{
			$data['ct_pointsperrace'] = number_format($data['ct_points'] / $data['ct_starts'], 2);
			$mode = $on ? 'On' : 'Off';
			include $skin_dir . 'tpl/standingEntry.tpl';
			$on = !$on;
		}
		
		include $skin_dir . 'tpl/tableEnd.tpl';
		
	}
	
}

?>